package com.ruoyi.common.api.impl;

import com.ruoyi.common.api.IBaseService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;

import java.util.Map;

public abstract class AbstractService<T> implements IBaseService<T> {

    private ProcessEngine processEngine;

    public void setProcessEngine(ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    //正向提交流程
    public int submitForNext(T record) {
        return 0;
    }

    //退回流程
    public int submitForback(T record) {
        return 0;
    }

    //流程跟踪
    public String traceFlow(String docUnid) {

        return "";
    }

    //通用提交流程
    private String submitFlow(boolean newFlag, Map<String, Object> submitParams) {
        if (newFlag) {
            //新流程
            String processKey = (String) submitParams.get("processKey");
            RuntimeService runtimeService = processEngine.getRuntimeService();
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey);
            return processInstance.getProcessInstanceId();
        } else {
            //老流程
            TaskService taskService = processEngine.getTaskService();
            String taskId = (String) submitParams.get("taskId");
            taskService.complete(taskId, submitParams);
            return "1";
        }
    }

}
